<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>验证码测试页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 50px auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .captcha-section {
            margin: 20px 0;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
            background-color: #fafafa;
        }
        .captcha-image {
            border: 2px solid #007bff;
            border-radius: 5px;
            margin: 10px 0;
            cursor: pointer;
        }
        button {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            margin: 5px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .info {
            background-color: #e7f3ff;
            border: 1px solid #b3d9ff;
            border-radius: 5px;
            padding: 15px;
            margin: 20px 0;
        }
        .error {
            background-color: #ffe7e7;
            border: 1px solid #ffb3b3;
            color: #d00;
        }
        .success {
            background-color: #e7ffe7;
            border: 1px solid #b3ffb3;
            color: #0a0;
        }
        input[type="text"] {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 3px;
            font-size: 16px;
            width: 150px;
        }
        .form-group {
            margin: 15px 0;
        }
        label {
            display: inline-block;
            width: 120px;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔐 图片验证码测试</h1>
        
        <div class="info">
            <h3>功能说明：</h3>
            <ul>
                <li>点击"生成验证码"按钮获取新的验证码图片</li>
                <li>验证码包含4位字符（字母和数字）</li>
                <li>每次生成的验证码都是随机的</li>
                <li>验证码图片包含干扰线和干扰点，提高安全性</li>
            </ul>
        </div>

        <div class="captcha-section">
            <h3>验证码生成测试</h3>
            <div class="form-group">
                <button onclick="generateCaptcha()">🔄 生成验证码</button>
                <button onclick="refreshCaptcha()">🆕 刷新验证码</button>
            </div>
            
            <div class="form-group">
                <label>验证码图片：</label>
                <div id="captcha-container">
                    <p>点击"生成验证码"按钮获取验证码</p>
                </div>
            </div>
            
            <div class="form-group">
                <label>验证码Key：</label>
                <span id="captcha-key">-</span>
            </div>
            
            <div class="form-group">
                <label>输入验证码：</label>
                <input type="text" id="captcha-input" placeholder="请输入验证码" maxlength="4">
                <button onclick="verifyCaptcha()">✅ 验证</button>
            </div>
            
            <div id="result-message"></div>
        </div>

        <div class="captcha-section">
            <h3>登录测试</h3>
            <div class="form-group">
                <label>用户名：</label>
                <input type="text" id="username" value="admin" placeholder="用户名">
            </div>
            <div class="form-group">
                <label>密码：</label>
                <input type="password" id="password" value="123456" placeholder="密码">
            </div>
            <div class="form-group">
                <label>验证码：</label>
                <input type="text" id="login-captcha" placeholder="验证码" maxlength="4">
                <button onclick="testLogin()">🚀 测试登录</button>
            </div>
            <div id="login-result"></div>
        </div>
    </div>

    <script>
        let currentCaptchaKey = '';
        const API_BASE = 'http://localhost:8080/api';

        async function generateCaptcha() {
            try {
                const response = await fetch(`${API_BASE}/auth/captcha`);
                const data = await response.json();
                
                if (data.code === 200) {
                    currentCaptchaKey = data.data.key;
                    document.getElementById('captcha-key').textContent = currentCaptchaKey;
                    document.getElementById('captcha-container').innerHTML = 
                        `<img src="${data.data.image}" alt="验证码" class="captcha-image" onclick="refreshCaptcha()" title="点击刷新验证码">`;
                    showMessage('验证码生成成功！', 'success');
                } else {
                    showMessage('验证码生成失败：' + data.message, 'error');
                }
            } catch (error) {
                showMessage('请求失败：' + error.message, 'error');
            }
        }

        function refreshCaptcha() {
            generateCaptcha();
        }

        async function verifyCaptcha() {
            const inputCode = document.getElementById('captcha-input').value;
            if (!inputCode) {
                showMessage('请输入验证码', 'error');
                return;
            }
            if (!currentCaptchaKey) {
                showMessage('请先生成验证码', 'error');
                return;
            }

            try {
                const response = await fetch(`${API_BASE}/auth/verify-captcha`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: `key=${currentCaptchaKey}&code=${inputCode}`
                });
                const data = await response.json();
                
                if (data.code === 200) {
                    showMessage('验证码验证成功！', 'success');
                } else {
                    showMessage('验证码验证失败：' + data.message, 'error');
                }
            } catch (error) {
                showMessage('验证请求失败：' + error.message, 'error');
            }
        }

        async function testLogin() {
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const captcha = document.getElementById('login-captcha').value;

            if (!username || !password) {
                showLoginMessage('请输入用户名和密码', 'error');
                return;
            }

            try {
                const loginData = {
                    username: username,
                    password: password
                };

                if (captcha && currentCaptchaKey) {
                    loginData.captcha = captcha;
                    loginData.captchaKey = currentCaptchaKey;
                }

                const response = await fetch(`${API_BASE}/auth/login`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(loginData)
                });
                const data = await response.json();
                
                if (data.code === 200) {
                    showLoginMessage('登录成功！Token: ' + data.data.token.substring(0, 50) + '...', 'success');
                } else {
                    showLoginMessage('登录失败：' + data.message, 'error');
                    // 登录失败后刷新验证码
                    generateCaptcha();
                }
            } catch (error) {
                showLoginMessage('登录请求失败：' + error.message, 'error');
            }
        }

        function showMessage(message, type) {
            const resultDiv = document.getElementById('result-message');
            resultDiv.innerHTML = `<div class="info ${type}">${message}</div>`;
        }

        function showLoginMessage(message, type) {
            const resultDiv = document.getElementById('login-result');
            resultDiv.innerHTML = `<div class="info ${type}">${message}</div>`;
        }

        // 页面加载时自动生成一个验证码
        window.onload = function() {
            generateCaptcha();
        };
    </script>
</body>
</html>